package com.ruoyi.vplat.websocket.handler;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.websocket.handler.AbstractWebsocketMsgHandler;
import com.ruoyi.common.websocket.server.WebSocketUtils;
import com.ruoyi.vplat.service.IVplatGatewayService;
import com.ruoyi.vplat.service.IVplatVideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
@Slf4j
public class WebsocketMsgHandler10055 extends AbstractWebsocketMsgHandler {
	@Autowired
	private ApplicationEventPublisher eventPublisher;
	@Autowired
	private IVplatGatewayService gatewayService;
	@Autowired
	private IVplatVideoService vplatVideoService;
	@Override
	public void invoke(JSONObject message) {
		JSONObject param = message.getJSONObject("param");
		JSONObject command = message.getJSONObject("command");
		String szClientId = command.getString("szClientId");
		Integer iSeqNum = command.getInteger("iSeqNum");
		Integer iProtocol = command.getInteger("iProtocol");
		String szDevId = param.getString("szDevId");
		Integer playId = param.getInteger("lPlayId");
		JSONObject result = vplatVideoService.stopHistoryRecord(new HashMap<String, Object>(){{
			put("protocol", iProtocol);
			put("deviceNo", szDevId);
			put("seqNum", iSeqNum);
			put("clientId", szClientId);
			put("playId", playId);
		}});

		JSONObject c = result.getJSONObject("command");
		if(c == null) {
			int iCommand = command.getIntValue("iCommand") + 1;
			c = new JSONObject(command);
			c.put("iCommand", iCommand);
			result.put("command", c);
		}

		WebSocketUtils.sendMessage(szClientId, result);
	}
}
